博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页操作
阅读量:5885 次
发布时间:2019-06-19

本文共 6448 字,大约阅读时间需要 21 分钟。

1、一般先封装一个用来显示分页的vo类

public class PageBean
{ //当前页 private int currentPage; //当前页显示的条数 private int currentCount; //总条数 private int totalCount; //总页数 private int totalPage; //每页显示的数据 private List
productList = new ArrayList
(); public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getCurrentCount() { return currentCount; } public void setCurrentCount(int currentCount) { this.currentCount = currentCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List
getProductList() { return productList; } public void setProductList(List
productList) { this.productList = productList; }}

Jsp

Servlet

//获得你想跳的页码 如果第一次访问列表,则currentPageStr为空,默认第一页String currentPageStr = request.getParameter("currentPage");if(currentPageStr==null) currentPageStr="1";int currentPage = Integer.parseInt(currentPageStr);//默认每页显示12条int currentCount = 12;PageBean
pageBean = null;try { pageBean = service.findPageBean(currentPage,currentCount);} catch (SQLException e) { e.printStackTrace();}request.setAttribute("pageBean", pageBean);request.getRequestDispatcher("/product_list.jsp").forward(request, response);

Service

//分页操作public PageBean findPageBean(int currentPage,int currentCount) throws SQLException  {    ProductDao dao = new ProductDao();        //目的:就是想办法封装一个PageBean 并返回    PageBean pageBean = new PageBean();    //1、当前页 currentPage;    pageBean.setCurrentPage(currentPage);    //2、当前页显示的条数 currentCount;    pageBean.setCurrentCount(currentCount);    //3、总条数 totalCount;    int totalCount = dao.getTotalCount();    pageBean.setTotalCount(totalCount);    //4、总页数 totalPage;    /*     * 总条数        当前页显示的条数    总页数     * 10                 4             3     * 11                 4             3     * 12                 4             3     * 13                 4             4     *      * 公式:总页数=Math.ceil(总条数/当前显示的条数)     *      */    int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);    pageBean.setTotalPage(totalPage);    //5、每页显示的数据private List
productList = new ArrayList
(); /* * 页数与limit起始索引的关系 * 例如 每页显示4条 * 页数 起始索引 每页显示条数 * 1 0 4 * 2 4 4 * 3 8 4 * 4 12 4 * * 索引index = (当前页数-1)*每页显示的条数 * */ int index = (currentPage-1)*currentCount; List
productList = dao.findProductListForPageBean(index,currentCount); pageBean.setProductList(productList); return pageBean;}

Dao

//获得全部的商品条数public int getTotalCount() throws SQLException {    QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());    String sql = "select count(*) from product";    Long query = (Long) runner.query(sql, new ScalarHandler());    return query.intValue();}//获得分页的商品数据public List
findProductListForPageBean(int index,int currentCount) throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from product limit ?,?"; return runner.query(sql, new BeanListHandler
(Product.class), index,currentCount);}

另一种封装PageBean的方法

public class PageBean {    private Integer currentPage;    private Integer pageSize;    private Integer totalCount;    private Integer totalPage;    private List list;        public PageBean(Integer currentPage, Integer pageSize, Integer totalCount) {                this.currentPage = currentPage;        this.pageSize = pageSize;        this.totalCount = totalCount;                if(this.currentPage == null){            this.currentPage = 1;        }                if(this.pageSize == null){            this.pageSize = 3;        }                this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;                if(this.currentPage < 1){            this.currentPage = 1;        }                if(this.currentPage > this.totalPage){            this.currentPage = this.totalPage;        }    }        public int getStart(){        return (this.currentPage-1)*this.pageSize;    }        public Integer getCurrentPage() {        return currentPage;    }    public void setCurrentPage(Integer currentPage) {        this.currentPage = currentPage;    }    public Integer getPageSize() {        return pageSize;    }    public void setPageSize(Integer pageSize) {        this.pageSize = pageSize;    }    public Integer getTotalCount() {        return totalCount;    }    public void setTotalCount(Integer totalCount) {        this.totalCount = totalCount;    }    public Integer getTotalPage() {        return totalPage;    }    public void setTotalPage(Integer totalPage) {        this.totalPage = totalPage;    }    public List getList() {        return list;    }    public void setList(List list) {        this.list = list;    }    }

 

转载于:https://www.cnblogs.com/ms-grf/p/7234117.html

你可能感兴趣的文章
webpack多页应用架构系列(七):开发环境、生产环境傻傻分不清楚?
查看>>
笨办法学C 练习1:启用编译器
查看>>
树的总结--树的性质(树的深度) leetcode
查看>>
nagios短信报警(飞信fetion20080522004-linrh4)
查看>>
【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!...
查看>>
linux 将大文件分成小文件
查看>>
CCNA- 距离矢量路由协议学习
查看>>
企业实践用户邮箱导入/导出(第2部分)
查看>>
我的友情链接
查看>>
如何学习Linux命令-初级篇
查看>>
从Oracle Public Yum为Oracle Linux建立本地的Yum源
查看>>
在 SELECT 查询中使用表表达式
查看>>
静态路由和默认路由
查看>>
谈一谈Spring-Mybatis在多数据源配置上的坑
查看>>
【精益生产】车间现场管理的八大浪费
查看>>
关于阿里开发者招聘节 |这5道笔试真题 你会吗!???
查看>>
C#的异常处理机制
查看>>
vsftp:500 OOPS: could not bind listening IPv4 sock
查看>>
Linux安装BTCPayServer并设置比特币BTC和Lightning支付网关
查看>>
Python 的 with 语句
查看>>